package jwt

import (
	"fmt"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"net/http"
	"twwch/k8s-admin/common/code"
	"twwch/k8s-admin/common/jwtAuth"
)

// 允许跨域
func NewJwt() gin.HandlerFunc {
	return func(c *gin.Context) {
		headers := c.Request.Header
		log.WithField("Request", fmt.Sprint(c.Request.URL.Path)).Error(c, c.Request)
		token := headers.Get("token")
		if ok, err := jwtAuth.CheckToken(token); err != nil || !ok {
			if err != nil {
				log.WithField("token expire", token).Error(c, err)
			}
			c.JSON(http.StatusOK, gin.H{
				"code":    code.CODE_AUTH_TOKEN_EXPIRE,
				"message": code.ErrCodeMessage(code.CODE_AUTH_TOKEN_EXPIRE),
				"data":    nil,
			})
			c.Abort()
			return
		}
	}
}
